package com.example.leo.mycharmingstudent.a0908;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.leo.mycharmingstudent.R;
import com.example.leo.mycharmingstudent.a0905.ToastUtil;
import com.google.gson.Gson;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class LoginActivity extends AppCompatActivity {

    private static final String TAG = "LoginActivity";

    //用户名输入框
    private EditText etUsername;
    //密码输入框
    private EditText etPassword;
    //注册按钮
    private TextView tvRegister;
    //登录按钮
    private TextView tvLogin;
    private Gson mGson;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);

        findViews();
        initData();
        initEvent();
    }

    private void findViews() {
        etUsername = findViewById(R.id.et_username_activity_login);
        etPassword = findViewById(R.id.et_password_activity_login);
        tvRegister = findViewById(R.id.tv_register_activity_login);
        tvLogin = findViewById(R.id.tv_login_activity_login);
    }

    private void initData() {
        mGson = new Gson();
    }

    private void initEvent() {
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
        tvLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                login();
            }
        });
    }

    /**
     * 登录  POST请求   FormBody
     */
    private void login() {
        //1.创建客户端
        OkHttpClient client = new OkHttpClient();
        //创建FormBody
        FormBody.Builder formBodyBuilder = new FormBody.Builder();
        FormBody formBody = formBodyBuilder
                .add("username", etUsername.getText().toString())
                .add("password", etPassword.getText().toString())
                .build();
        //2.创建构建者
        Request.Builder builder = new Request.Builder();
        builder.url("https://www.wanandroid.com/user/login")
                .post(formBody);
        //3.创建Request
        Request request = builder.build();
        //4.创建Call
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Log.e(TAG, e.getMessage(), e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                //response.body().string();也是在子线程中进行的
                String result = response.body().string();
                Log.e(TAG, result);
                final LoginEntity loginEntity = mGson.fromJson(result, LoginEntity.class);
                final int errorCode = loginEntity.getErrorCode();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        if (errorCode == 0) {
                            //接口正常访问，获取成功数据
                            ToastUtil.showToast(LoginActivity.this, "登录成功");
                        } else {
                            //接口未正常访问，可能用户名或密码出错
                            ToastUtil.showToast(LoginActivity.this, loginEntity.getErrorMsg());
                        }
                    }
                });

                LoginEntity.DataBean data = loginEntity.getData();
                //演示@SerializedName("id")注解的使用
                /*final long ididid = data.getIdidid();
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        ToastUtil.showToast(LoginActivity.this,String.valueOf(ididid));
                    }
                });*/
            }
        });
    }


    /**
     * 注册
     */
    private void register() {

    }

}
